package com.iuie.ns.frame.utils.httpclients;

import java.util.LinkedHashMap;
import java.util.Map;

public class HttpParams {

	private String charset;
	
	private ContentType contentType;
	
	private String url;
	
	private HttpRequestMethod method;
	
	private Object params;
	
	private Map<String, String> headers;
	
	private int socketTimeout;
	
	private int connectTimeout;
	
	private HttpParams() {
		
	}
	
	private HttpParams(String url) {
		this.url = url;
	}
	
	public HttpParams charset(String charset) {
		this.charset = charset;
		return this;
	}
	
	public String charset() {
		if (this.charset == null) {
			return "utf-8";
		}
		return this.charset;
	}
	
	public HttpParams contentType(ContentType contentType) {
		this.contentType = contentType;
		return this;
	}
	
	public ContentType contentType() {
		return this.contentType;
	}
	
	public HttpParams url(String url) {
		this.url = url;
		return this;
	}
	
	public String url() {
		return this.url;
	}
	
	public HttpRequestMethod method() {
		if (this.method == null) {
			return HttpRequestMethod.GET;
		}
		return this.method;
	}
	
	public HttpParams method(HttpRequestMethod method) {
		this.method = method;
		return this;
	}
	
	public static HttpParams newInstance(String url) {
		return new HttpParams(url);
	}
	
	public static HttpParams newInstance() {
		return new HttpParams();
	}
	
	@SuppressWarnings("unchecked")
	public HttpParams addParam(String key, Object object) {
		if (this.params == null) {
			this.params = new LinkedHashMap<>();
		}
		((Map<String, Object>)this.params).put(key, object);
		return this;
	}
	
	public HttpParams params(Map<String, Object> params) {
		this.params = params;
		return this;
	}
	
	public Object params() {
		return this.params;
	}
	
	public HttpParams addHeader(String key, String h) {
		if (this.headers == null) {
			this.headers = new LinkedHashMap<>();
		}
		this.headers.put(key, h);
		return this;
	}
	
	public HttpParams headers(Map<String, String> headers) {
		this.headers = headers;
		return this;
	}
	
	public Map<String, String> headers() {
		if (this.headers == null) {
			return new LinkedHashMap<>();
		}
		return this.headers;
	}
	
	public HttpParams objectParams(Object params) {
		this.params = params;
		return this;
	}
	
	public static HttpParams useTemplate(HttpParamsTemplate template) {
		return template.getHttpParams();
	}
	
	public HttpParams connectTimeout(int connectTimeout) {
		this.connectTimeout = connectTimeout;
		return this;
	}
	
	public int connectTimeout() {
		if (this.socketTimeout == 0) {
			return 5000;
		}
		return this.connectTimeout;
	}
	
	public HttpParams socketTimeout(int socketTimeout) {
		this.socketTimeout = socketTimeout;
		return this;
	}
	
	public int socketTimeout() {
		if (this.socketTimeout == 0) {
			return 5000;
		}
		return this.socketTimeout;
	}
	
	
	
}